<?php
// 本类由系统自动生成，仅供测试用途
class LoginAction extends Action {
    public function index(){
    	
    	if(session_is_registered('username')){
			$this->error('您已经登陆！','__APP__/index');
			return;
    	}
    	
    	$error = array(
    		'type' => 'information',
    		'text' => '请正确填写表单',
    	);
    	
    	
    	if($this->_post('login') == 'a'){
    		
			//获取用户名及密码
		    $UserName = $this->_post('username');
		    $PassWord = $this->_post('password');
		    
		    
		    //验证用户名及密码是否为空
		    if(empty($UserName) || empty($PassWord)){
		    	$error['type'] = 'error';
		    	$error['text'] = '用户名或密码不能为空';
			    $this->assign('error',$error);
			    $this->display();
			
			//验证验证码    
		    }elseif(session('verify') != md5($_POST['verify'])) {
			    $error['type'] = 'error';
		    	$error['text'] = '验证码错误';
			    $this->assign('error',$error);
			    $this->display();
			    
			//验证用户是否存在
			}else{
				
				$User = M('User');
			    $DataUser = $User->where('`username` = '."'$UserName'")->find();
			    
			    if(empty($DataUser)){
				    $error['type'] = 'error';
			    	$error['text'] = '用户不存在';
				    $this->assign('error',$error);
				    $this->display();
			    }elseif($DataUser['password'] == md5($PassWord)){
			    	
			    	$data = array('utime'=>time(),);
			    	$User->where('id='.$DataUser['id'])->data($data)->save();
			    	
			    	//检测SESSION是否存在
				    if(!session_is_registered('username')){
				    	//设置USERNAME
			    		$_SESSION['username'] = $UserName;
			    		//设置UID
			    		$_SESSION['uid'] = $DataUser['id'];
			    	}
			    	
			    	//跳转至首页
				    $this->success('登陆成功', '__APP__/Index/index');

				    
			    }else{
				    $error['type'] = 'error';
			    	$error['text'] = '用户或密码错误';
				    $this->assign('error',$error);
				    $this->display();
			    }
			    
		    }

	    }else{
	    	$this->assign('error', $error);
			$this->display(); 
			
	    }

    }
    
    public function logout(){
    	$this->success('退出登陆成功', '__URL__/index');
		unset($_SESSION['username']);
		unset($_SESSION['uid']);
    }
    

    //检测是否已经登陆
    public function isLogin(){
		if(!session_is_registered('username')){
			$this->error('您尚未登陆！','__APP__/Login/index');
    	}
	}

    
    public function verify(){
    	import('ORG.Util.String');
    	import('ORG.Util.Image');
    	Image::buildImageVerify(4,1,"png",80,28);
    }
}
